package elizaengine;

import java.util.ArrayList;
import java.util.Random;

import elizascript.ScriptInterop;

/**
 * The Engine class manipulates the users input according to the rules
 * retrieved from the ScriptInterop class to form the Eliza response.
 * 
 * @author Caroline
 *
 */
public class Engine {
	
	private static boolean shouldQuit = true;
	
	// provides access to the Eliza script
	private ScriptInterop script;

	// provides ability to apply substitution rules
	private SubstitutionRules substitutionRules;
	private ResponseGenerator responseGenerator;
	
	public Engine(ScriptInterop script) {
		this.script = script;
		substitutionRules = new SubstitutionRules(script);
	}

	public String initialise() {
		Random random = new Random();
		ArrayList<String> welcomeMessages = script.getWelcomeMessage();
		int chosenMessage = random.nextInt(welcomeMessages.size());
		return welcomeMessages.get(chosenMessage);
	}

	// returns the response generated to a specific input
	public String getElizaResponse(String input) {
		String preparedInput = substitutionRules.applyPresubstitutionRule(input);
		responseGenerator = new ResponseGenerator(script, preparedInput);
		String response = responseGenerator.generateResponse();
		return response;
	}

	
	// defines when the program stops running
	public boolean shouldQuit() {
		return shouldQuit;
	}
	
	public static void quit(){
		shouldQuit = false;
	}

}
